{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第 4 节　认识 numpy 与 pandas\n",
    "\n",
    "## 第 2 章　Python 与 Jupyter Notebook 基础｜用 Python 动手学统计学"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1. 导入用于分析的功能"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:30.908988Z",
     "end_time": "2024-04-15T19:56:31.390071Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3. 实现：列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "[1, 2, 3, 4, 5]"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample_list = [1, 2, 3, 4, 5]\n",
    "sample_list"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.390071Z",
     "end_time": "2024-04-15T19:56:31.405770Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5. 实现：数组"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3, 4, 5])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample_array = np.array([1, 2, 3, 4, 5])\n",
    "sample_array"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.405770Z",
     "end_time": "2024-04-15T19:56:31.462996Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "array([3, 4, 5, 6, 7])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample_array + 2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.426084Z",
     "end_time": "2024-04-15T19:56:31.467931Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 2,  4,  6,  8, 10])"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample_array * 2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.443299Z",
     "end_time": "2024-04-15T19:56:31.467931Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "array(['1', '2', 'A'], dtype='<U11')"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1, 2, 'A'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.467931Z",
     "end_time": "2024-04-15T19:56:31.524588Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1,  2,  3,  4,  5],\n       [ 6,  7,  8,  9, 10]])"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵\n",
    "sample_array_2 = np.array(\n",
    "    [[1, 2, 3, 4, 5],\n",
    "     [6, 7, 8, 9, 10]])\n",
    "sample_array_2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.488262Z",
     "end_time": "2024-04-15T19:56:31.525114Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "(2, 5)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample_array_2.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.497141Z",
     "end_time": "2024-04-15T19:56:31.526092Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 6. 实现：生成等差数列的方法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3, 4, 5])"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(start=1, stop=6, step=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.505910Z",
     "end_time": "2024-04-15T19:56:31.526591Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0.1, 0.3, 0.5, 0.7])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(start=0.1, stop=0.8, step=0.2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.525114Z",
     "end_time": "2024-04-15T19:56:31.546889Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0.1, 0.3, 0.5, 0.7])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0.1, 0.8, 0.2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.539116Z",
     "end_time": "2024-04-15T19:56:31.605794Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 7. 实现：多种生成数组的方式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "array(['A', 'A', 'A', 'A', 'A'], dtype='<U1')"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元素相同的数组\n",
    "np.tile(\"A\", 5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.566714Z",
     "end_time": "2024-04-15T19:56:31.605794Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 0, 0, 0])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 存放 4 个 0\n",
    "np.tile(0, 4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.573640Z",
     "end_time": "2024-04-15T19:56:31.637371Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0., 0., 0., 0.])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只有 0 的数组\n",
    "np.zeros(4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.595390Z",
     "end_time": "2024-04-15T19:56:31.638873Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0., 0., 0.],\n       [0., 0., 0.]])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维数组\n",
    "np.zeros([2, 3])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.602180Z",
     "end_time": "2024-04-15T19:56:31.638873Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1., 1., 1.])"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只有 1 的数组\n",
    "np.ones(3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.621501Z",
     "end_time": "2024-04-15T19:56:31.638873Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 8. 实现：切片"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3, 4, 5])"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一维数组\n",
    "d1_array = np.array([1, 2, 3, 4, 5])\n",
    "d1_array"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.638873Z",
     "end_time": "2024-04-15T19:56:31.699345Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取得第一个元素\n",
    "d1_array[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.659081Z",
     "end_time": "2024-04-15T19:56:31.699345Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "array([2, 3])"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取索引中的 1 号和 2 号元素\n",
    "d1_array[1:3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.675338Z",
     "end_time": "2024-04-15T19:56:31.699345Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1,  2,  3,  4,  5],\n       [ 6,  7,  8,  9, 10]])"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维数组\n",
    "d2_array = np.array(\n",
    "    [[1, 2, 3, 4, 5],\n",
    "     [6, 7, 8, 9, 10]])\n",
    "d2_array"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.685284Z",
     "end_time": "2024-04-15T19:56:31.699345Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "4"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2_array[0, 3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.699345Z",
     "end_time": "2024-04-15T19:56:31.715584Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "array([8, 9])"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2_array[1, 2:4]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.715584Z",
     "end_time": "2024-04-15T19:56:31.768616Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 9. 实现：数据帧"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col1  col2 col3\n",
      "0     1     2    A\n",
      "1     2     4    B\n",
      "2     3     6    C\n",
      "3     4     8    D\n",
      "4     5    10    E\n"
     ]
    }
   ],
   "source": [
    "sample_df = pd.DataFrame({\n",
    "    'col1': sample_array,\n",
    "    'col2': sample_array * 2,\n",
    "    'col3': [\"A\", \"B\", \"C\", \"D\", \"E\"]\n",
    "})\n",
    "print(sample_df)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.735379Z",
     "end_time": "2024-04-15T19:56:31.768616Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "   col1  col2 col3\n0     1     2    A\n1     2     4    B\n2     3     6    C\n3     4     8    D\n4     5    10    E",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col1</th>\n      <th>col2</th>\n      <th>col3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>4</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>6</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4</td>\n      <td>8</td>\n      <td>D</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>10</td>\n      <td>E</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample_df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.750828Z",
     "end_time": "2024-04-15T19:56:31.768616Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "   col1 col2\n0     1    A\n1     2    A\n2     3    B\n3     4    B\n4     5    C\n5     6    C",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>col1</th>\n      <th>col2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>6</td>\n      <td>C</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_data = pd.read_csv('2-4-1-sample_data.csv')\n",
    "file_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.763853Z",
     "end_time": "2024-04-15T19:56:31.790557Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "pandas.core.frame.DataFrame"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(file_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.790057Z",
     "end_time": "2024-04-15T19:56:31.800576Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 11. 实现：连接数据帧"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "df_1 = pd.DataFrame({\n",
    "    'col1': np.array([1, 2, 3]),\n",
    "    'col2': np.array([\"A\", \"B\", \"C\"])\n",
    "})\n",
    "df_2 = pd.DataFrame({\n",
    "    'col1': np.array([4, 5, 6]),\n",
    "    'col2': np.array([\"D\", \"E\", \"F\"])\n",
    "})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.800576Z",
     "end_time": "2024-04-15T19:56:31.867263Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col1 col2\n",
      "0     1    A\n",
      "1     2    B\n",
      "2     3    C\n",
      "0     4    D\n",
      "1     5    E\n",
      "2     6    F\n"
     ]
    }
   ],
   "source": [
    "# 在纵向上连接\n",
    "print(pd.concat([df_1, df_2]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.822239Z",
     "end_time": "2024-04-15T19:56:31.932661Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col1 col2  col1 col2\n",
      "0     1    A     4    D\n",
      "1     2    B     5    E\n",
      "2     3    C     6    F\n"
     ]
    }
   ],
   "source": [
    "# 在横向上连接\n",
    "print(pd.concat([df_1, df_2], axis=1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.841903Z",
     "end_time": "2024-04-15T19:56:31.938669Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 12. 实现：数据帧的列操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col1  col2 col3\n",
      "0     1     2    A\n",
      "1     2     4    B\n",
      "2     3     6    C\n",
      "3     4     8    D\n",
      "4     5    10    E\n"
     ]
    }
   ],
   "source": [
    "# 对象数据\n",
    "print(sample_df)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.871698Z",
     "end_time": "2024-04-15T19:56:31.996866Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     2\n",
      "1     4\n",
      "2     6\n",
      "3     8\n",
      "4    10\n",
      "Name: col2, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "# 按列名获取数据\n",
    "print(sample_df.col2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.892480Z",
     "end_time": "2024-04-15T19:56:32.030591Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     2\n",
      "1     4\n",
      "2     6\n",
      "3     8\n",
      "4    10\n",
      "Name: col2, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "print(sample_df[\"col2\"])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.915277Z",
     "end_time": "2024-04-15T19:56:32.043300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col2 col3\n",
      "0     2    A\n",
      "1     4    B\n",
      "2     6    C\n",
      "3     8    D\n",
      "4    10    E\n"
     ]
    }
   ],
   "source": [
    "print(sample_df[[\"col2\", \"col3\"]])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.933660Z",
     "end_time": "2024-04-15T19:56:32.043300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col2 col3\n",
      "0     2    A\n",
      "1     4    B\n",
      "2     6    C\n",
      "3     8    D\n",
      "4    10    E\n"
     ]
    }
   ],
   "source": [
    "# 删除指定的列\n",
    "print(sample_df.drop(\"col1\", axis=1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.951280Z",
     "end_time": "2024-04-15T19:56:32.043300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 13. 实现：数据帧的行操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col1  col2 col3\n",
      "0     1     2    A\n",
      "1     2     4    B\n",
      "2     3     6    C\n"
     ]
    }
   ],
   "source": [
    "# 获取前 3 行\n",
    "print(sample_df.head(n=3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.976811Z",
     "end_time": "2024-04-15T19:56:32.043300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col1  col2 col3\n",
      "0     1     2    A\n"
     ]
    }
   ],
   "source": [
    "# 获取第 1 行\n",
    "print(sample_df.query('index == 0'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:31.999693Z",
     "end_time": "2024-04-15T19:56:32.154349Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col1  col2 col3\n",
      "0     1     2    A\n"
     ]
    }
   ],
   "source": [
    "# 通过多种条件获取数据\n",
    "print(sample_df.query('col3 == \"A\"'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:32.023422Z",
     "end_time": "2024-04-15T19:56:32.154848Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col1  col2 col3\n",
      "0     1     2    A\n",
      "3     4     8    D\n"
     ]
    }
   ],
   "source": [
    "# 按 OR 条件获取数据\n",
    "print(sample_df.query('col3 == \"A\" | col3 == \"D\"'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:32.043300Z",
     "end_time": "2024-04-15T19:56:32.191076Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: [col1, col2, col3]\n",
      "Index: []\n"
     ]
    }
   ],
   "source": [
    "# 按 AND 条件获取数据\n",
    "print(sample_df.query('col3 == \"A\" & col1 == 3'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:32.064163Z",
     "end_time": "2024-04-15T19:56:32.191076Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   col2 col3\n",
      "0     2    A\n"
     ]
    }
   ],
   "source": [
    "# 同时指定行和列的条件\n",
    "print(sample_df.query('col3 == \"A\"')[[\"col2\", \"col3\"]])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:32.085416Z",
     "end_time": "2024-04-15T19:56:32.191076Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 14. 补充：序列"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "pandas.core.frame.DataFrame"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(sample_df)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:32.104182Z",
     "end_time": "2024-04-15T19:56:32.191076Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "pandas.core.series.Series"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(sample_df.col1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:32.131896Z",
     "end_time": "2024-04-15T19:56:32.191076Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "numpy.ndarray"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为数组\n",
    "type(np.array(sample_df.col1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:32.154848Z",
     "end_time": "2024-04-15T19:56:32.191076Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "numpy.ndarray"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(sample_df.col1.values)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T19:56:32.171560Z",
     "end_time": "2024-04-15T19:56:32.288001Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
